
import { Component, Vue } from 'vue-property-decorator';
import { superFun } from '@/utils/Annotate'
/**
 * 这是一个弹窗父容器mixin类
 *
 * @Function onCloseDialog 关闭弹窗
 * @Function setDialogHidden 设置弹窗是否隐藏
 *
 * @export
 * @class mixinDialogParent
 * @extends {Vue}
 */
@Component({
})
export class mixinDialogParent extends Vue {
    /**
     * 关闭弹窗
     *
     * @param {string} [dialogName='dialogForm'] 弹窗ref
     * @memberof mixinDialogParent
     */
    @superFun()
    onCloseDialog(dialogName: string = 'dialogForm') {
        // console.log('onCloseDialog');
        const me = this as any;
        // 检查是否有closeBefore方法，如果有就执行
        me.beforecloseDialog && me.beforecloseDialog();
        // 关闭弹窗
        me.setDialogHidden(true, dialogName);
    }

    /**
     * 设置弹窗是否隐藏
     *
     * @param {boolean} v true 隐藏，false 显示
     * @param {string} [dialogName='dialogForm'] 弹窗ref
     * @memberof mixinDialogParent
     */
    setDialogHidden(v: boolean, dialogName: string = 'dialogForm') {
        (this.$refs[dialogName] as any).setHidden(v);
    }

    showDialog(dialogName: string = 'dialogForm') {
        this.setDialogHidden(false, dialogName);
    }

    hideDialog(dialogName: string = 'dialogForm') {
        this.setDialogHidden(true, dialogName);
    }

    /**
    * 提交数据成功后逻辑处理，会调用onCloseDialog方法
    *
    * @param {string} [dialogName='dialogForm'] 弹窗ref名称
    * @param {boolean} [isReLoad=true] 是否重载缓存数据，用于固定公用数据需要更新的场景
    * @memberof mixinDialogFormParent
    */
    onSubmitSuccess(dialogName: string = 'dialogForm', { isReLoad = true, data }: any = {}) {
        const me = this as any;
        me.onCloseDialog(dialogName);
        if (isReLoad) {
            me.loadView && me.loadView({
                dialogName,
                isAdd: !me.getDialogValue('isEdit', dialogName),
                data
            });
        }
    }

    /**
     * 设值弹窗值
     *
     * @param {string} key 字段名称
     * @param {*} data 字段值
     * @param {string} [dialogName='dialogForm'] 弹窗ref名称
     */
    setDialogValue(key: string, data: any, dialogName: string = 'dialogForm') {
        const me = this as any;
        me.$refs[dialogName].setValue(key, data);
    }

    /**
     * 获取弹窗值
     *
     * @param {string} key 字段名称
     * @param {string} [dialogName='dialogForm'] 弹窗ref名称
     * @memberof mixinDialogFormParent
     */
    getDialogValue(key: string, dialogName: string = 'dialogForm') {
        const me = this as any;
        return me.$refs[dialogName].getValue(key);
    }
}
